package com.pet.server.ext.action.article;

import java.util.ArrayList;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Article;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.service.ArticleService;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class List extends ActionBase{

	public Integer startIndex,itemCount;
	public String queryString,cate,age_cate,pet_cate;
	public String server_name;
	public Page page;
	public List(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		cate = params.getFieldValue("cate");
		age_cate = params.getFieldValue("age_cate");
		pet_cate = params.getFieldValue("pet_cate");
		startIndex = NumberUtil.getInteger(params.getFieldValue("startIndex"), 0);
		itemCount =  NumberUtil.getInteger(params.getFieldValue("itemCount"), 10);
		queryString = params.getFieldValue("queryString");
		server_name = params.getFieldValue("request.servername");
		System.out.println(server_name);
		
		page = new Page(startIndex, itemCount,queryString);
	}
	
	@Override
	public Result invokeService() throws ApiError {
		ArticleService articleService = new ArticleService();
		java.util.List<Constant> list = articleService.queryConstant(BaseAttribute.article_pcode);
		for(Constant c:list){
			c.setList(articleService.queryConstant(c.getCode()));
		}
		articleService.searchArticle(page, cate,age_cate,pet_cate);
		for(Article art:(java.util.List<Article>)page.getList()){
			java.util.List<String> images = new ArrayList<String>();
			//art.url="http://"+server_name+"/article/detail.do?article_id="+art.article_id;
			art.url="http://pet.zaiziha.com/article/detail.do?article_id="+art.article_id;
			if(BaseAttribute.view_type.grid.toString().equals("grid")){
				if(!StringUtil.isEmpty(art.image0)){images.add(art.image0);}
				if(!StringUtil.isEmpty(art.image1)){images.add(art.image1);}
				if(!StringUtil.isEmpty(art.image2)){images.add(art.image2);}
			}else if(BaseAttribute.view_type.small.toString().equals("small")){
				if(!StringUtil.isEmpty(art.image0)){images.add(art.image0);}
			}if(BaseAttribute.view_type.large.toString().equals("large")){
				if(!StringUtil.isEmpty(art.image0)){images.add(art.image0);}
			}
			art.images=images;
		}
		
		/**
		JSONArray r = new JSONArray();
		for(Article art:(java.util.List<Article>)page.getList()){
			JSONObject o = new JSONObject();
			o.put("title", art.title);
			o.put("article_id", art.article_id);
			o.put("ico_path", art.ico_path);
			o.put("cate", art.cate);
			o.put("description", art.description);
			o.put("create_time", DateUtil.getFormatDate(art.create_time,"yyyy-MM-dd"));
			r.add(o);
		}
		*/
		
		//JSONObject o = new JSONObject();
		//o.put("cate", BeanToJson.listTojson(list,"id","val","pcode"));
		
		
		Result result = new Result();
        //result.put("data", o);
        result.setPage(page,"text","image0","image1","image2","ico_path");
	    return result;
	}


}
